<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Hugo 0.82.0" />


<title>Rstudio-server docker 镜像配置 - 宇飞的世界</title>
<meta property="og:title" content="Rstudio-server docker 镜像配置 - 宇飞的世界">


  <link href='/favicon.ico' rel='icon' type='image/x-icon'/>



  








<link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css' rel='stylesheet' type='text/css' />



<link rel="stylesheet" href="/css/fonts.css" media="all">
<link rel="stylesheet" href="/css/main.css" media="all">



  </head>
  <body>
    <div class="wrapper">
      <header class="header">
        <nav class="nav">
  <a href="/" class="nav-logo">
    <img src="/images/logo.png"
         width="50"
         height="50"
         alt="Logo">
  </a>

  <ul class="nav-links">
    
    <li><a href="/about/">About</a></li>
    
    <li><a href="/categories/">Categories</a></li>
    
    <li><a href="/tags/">Tags</a></li>
    
    <li><a href="http://www.zhongyufei.com/Rbook/_book">R study</a></li>
    
  </ul>
</nav>

      </header>


<main class="content" role="main">

  <article class="article">
    
    <span class="article-duration">4 min read</span>
    

    <h1 class="article-title">Rstudio-server docker 镜像配置</h1>

    
    <span class="article-date">2021-06-09</span>
    

    <div class="article-content">
      
<script src="/2021/06/09/rstudio-server-dockerfile/index_files/accessible-code-block/empty-anchor.js"></script>
<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  background-color: #f8f8f8; }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #c4a000; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #000000; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #000000; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #000000; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
</style>

<div id="TOC">
<ul>
<li><a href="#r-docker-images">R docker images</a></li>
<li><a href="#dockerfile">dockerfile</a></li>
<li><a href="#构造镜像">构造镜像</a></li>
<li><a href="#使用镜像">使用镜像</a></li>
<li><a href="#保存镜像">保存镜像</a><ul>
<li><a href="#保存到-dokcer-hub">保存到 dokcer hub</a></li>
<li><a href="#本地保存使用">本地保存使用</a></li>
</ul></li>
<li><a href="#参考资料">参考资料</a></li>
</ul>
</div>

<div id="r-docker-images" class="section level2">
<h2>R docker images</h2>
<p>有时候需要使用 rstudio-server 的 docker 镜像快速测试，但是现有的 镜像不满足直接使用要求，故自己根据<a href="https://hub.docker.com/r/rocker/rstudio"><code>rocker/rstudio</code></a> 制作了一个符合自己使用要求的镜像。主要添加了 mssql 的 odbc 驱动，以及常用的数据处理 R 包，如<code>tidyverse</code>，<code>data.table</code>，<code>odbc</code>，<code>DBI</code>等 R 包。</p>
</div>
<div id="dockerfile" class="section level2">
<h2>dockerfile</h2>
<p>首先<code>mkdir</code>创建项目文件夹，cd 进入，创建 dockerfile 。</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1"></a><span class="fu">mkdir</span> rstudio-server </span>
<span id="cb1-2"><a href="#cb1-2"></a><span class="bu">cd</span> rstudio-server</span>
<span id="cb1-3"><a href="#cb1-3"></a><span class="fu">touch</span> dokcerfile</span>
<span id="cb1-4"><a href="#cb1-4"></a><span class="fu">nano</span> dockerfile</span></code></pre></div>
<p>在 dockerfile 中输入以下内容：</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1"></a><span class="ex">FROM</span> rocker/rstudio</span>
<span id="cb2-2"><a href="#cb2-2"></a></span>
<span id="cb2-3"><a href="#cb2-3"></a><span class="ex">RUN</span> sudo apt-get update </span>
<span id="cb2-4"><a href="#cb2-4"></a><span class="ex">RUN</span> apt-get install -y curl  git gnupg2 openjdk-8-jdk unixodbc-dev</span>
<span id="cb2-5"><a href="#cb2-5"></a></span>
<span id="cb2-6"><a href="#cb2-6"></a><span class="ex">RUN</span> curl https://packages.microsoft.com/keys/microsoft.asc <span class="kw">|</span> <span class="ex">apt-key</span> add -</span>
<span id="cb2-7"><a href="#cb2-7"></a><span class="ex">RUN</span> curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list <span class="op">&gt;</span> /etc/apt/sources.list.d/mssql-release.list</span>
<span id="cb2-8"><a href="#cb2-8"></a><span class="ex">RUN</span> sudo apt-get update</span>
<span id="cb2-9"><a href="#cb2-9"></a><span class="ex">RUN</span> ACCEPT_EULA=Y apt-get install -y --allow-unauthenticated msodbcsql17</span>
<span id="cb2-10"><a href="#cb2-10"></a><span class="ex">RUN</span> ACCEPT_EULA=Y apt-get install -y --allow-unauthenticated mssql-tools</span>
<span id="cb2-11"><a href="#cb2-11"></a><span class="ex">RUN</span> echo <span class="st">&#39;export PATH=&quot;$PATH:/opt/mssql-tools/bin&quot;&#39;</span> <span class="op">&gt;&gt;</span> ~/.bash_profile</span>
<span id="cb2-12"><a href="#cb2-12"></a><span class="ex">RUN</span> echo <span class="st">&#39;export PATH=&quot;$PATH:/opt/mssql-tools/bin&quot;&#39;</span> <span class="op">&gt;&gt;</span> ~/.bashrc</span>
<span id="cb2-13"><a href="#cb2-13"></a></span>
<span id="cb2-14"><a href="#cb2-14"></a>  </span>
<span id="cb2-15"><a href="#cb2-15"></a></span>
<span id="cb2-16"><a href="#cb2-16"></a><span class="ex">RUN</span> apt-get update -qq <span class="kw">&amp;&amp;</span> <span class="ex">apt-get</span> install -y \</span>
<span id="cb2-17"><a href="#cb2-17"></a>  libcurl4-gnutls-dev \</span>
<span id="cb2-18"><a href="#cb2-18"></a>  libxml2-dev \</span>
<span id="cb2-19"><a href="#cb2-19"></a>  openssl \</span>
<span id="cb2-20"><a href="#cb2-20"></a>  libssl-dev \</span>
<span id="cb2-21"><a href="#cb2-21"></a>  libmysqlclient-dev </span>
<span id="cb2-22"><a href="#cb2-22"></a>  </span>
<span id="cb2-23"><a href="#cb2-23"></a><span class="ex">RUN</span> R -e <span class="st">&quot;install.packages(c(&#39;odbc&#39;,&#39;DBI&#39;,&#39;tidyverse&#39;,&#39;lubridate&#39;,&#39;data.table&#39;,&#39;fs&#39;,&#39;blogdown&#39;,&#39;bookdown&#39;))&quot;</span></span>
<span id="cb2-24"><a href="#cb2-24"></a><span class="ex">RUN</span> R -e <span class="st">&quot;install.packages(c(&#39;shiny&#39;,&#39;collapse&#39;,&#39;reactable&#39;,&#39;future.apply&#39;,&#39;echarts4r&#39;,&#39;DT&#39;,&#39;devtools&#39;,&#39;furrr&#39;))&quot;</span></span></code></pre></div>
<blockquote>
<p>需要注意openssl的版本，版本太高连接mssql2014是会报错。OpenSSL 1.0.2k-fips 版本可以正常连接mssql-2014。</p>
</blockquote>
<p>降低版本</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1"></a><span class="ex">openssl</span> version</span>
<span id="cb3-2"><a href="#cb3-2"></a><span class="fu">wget</span> http://security.debian.org/debian-security/pool/updates/main/o/openssl/openssl_1.1.0k-1~deb9u1_amd64.deb</span>
<span id="cb3-3"><a href="#cb3-3"></a><span class="ex">dpkg</span> -i openssl_1.1.0k-1~deb9u1_amd64.deb</span></code></pre></div>
</div>
<div id="构造镜像" class="section level2">
<h2>构造镜像</h2>
<p>最后构建 docker 镜像，注意最后的点 ，表示用当前目录的 dockerfile 创建镜像， -t 表示创建镜像的名称。</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1"></a><span class="ex">docker</span> build -t namedvega/rdocker-rstudio:latest .</span></code></pre></div>
</div>
<div id="使用镜像" class="section level2">
<h2>使用镜像</h2>
<p>使用 rstudio 账户，密码为123456登录 rstudio-server</p>
<pre><code>docker run -d -p 8787:8787 -v $(pwd):/home/rstudio -e ROOT=TRUE -e PASSWORD=123456 rdocker/rstudio-server</code></pre>
<div class="sourceCode" id="cb6"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1"></a><span class="ex">docker</span> run -d -p 7777:8787 -v <span class="va">$(</span><span class="bu">pwd</span><span class="va">)</span>:/home/rstudio -e PASSWORD=123456 rdocker/rstudio-server</span></code></pre></div>
</div>
<div id="保存镜像" class="section level2">
<h2>保存镜像</h2>
<p>最后登录 docker ，将镜像保存到个人仓库中（需要有个人docker 账号）。</p>
<div id="保存到-dokcer-hub" class="section level3">
<h3>保存到 dokcer hub</h3>
<div class="sourceCode" id="cb7"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1"></a><span class="co"># 登录 docker</span></span>
<span id="cb7-2"><a href="#cb7-2"></a><span class="ex">docker</span> login</span>
<span id="cb7-3"><a href="#cb7-3"></a></span>
<span id="cb7-4"><a href="#cb7-4"></a><span class="co"># 查看镜像ID</span></span>
<span id="cb7-5"><a href="#cb7-5"></a><span class="ex">docker</span> images</span>
<span id="cb7-6"><a href="#cb7-6"></a></span>
<span id="cb7-7"><a href="#cb7-7"></a><span class="co"># 标记镜像</span></span>
<span id="cb7-8"><a href="#cb7-8"></a><span class="ex">docker</span> tag dockerimages id namedvega/rdocker-rstudio:latest</span>
<span id="cb7-9"><a href="#cb7-9"></a></span>
<span id="cb7-10"><a href="#cb7-10"></a><span class="co"># 推送镜像</span></span>
<span id="cb7-11"><a href="#cb7-11"></a><span class="ex">docker</span> push namedvega/rdocker-rstudio</span></code></pre></div>
</div>
<div id="本地保存使用" class="section level3">
<h3>本地保存使用</h3>
<p>上述的推送镜像会对带宽有一定要求，我们可以将镜像本地保存使用。</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1"></a><span class="ex">docker</span> save rdocker-rstudio <span class="op">&gt;</span> rdocker-rstudio.tar</span></code></pre></div>
<p>如果我们想要从存档中载入镜像，使用如下命令：</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1"></a><span class="ex">docker</span> load -- input rdocker-rstudio.tar</span></code></pre></div>
</div>
</div>
<div id="参考资料" class="section level2">
<h2>参考资料</h2>
<ol style="list-style-type: decimal">
<li><a href="https://www.jianshu.com/p/b4f029645ac4" class="uri">https://www.jianshu.com/p/b4f029645ac4</a></li>
</ol>
</div>

    </div>
  </article>

  


</main>

      <footer class="footer">
        <ul class="footer-links">
          <li>
            <a href="/index.xml" type="application/rss+xml" target="_blank">RSS feed</a>
          </li>
          <li>
            <a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备20035539号</a>
          </li>
        </ul>
      </footer>

    </div>
    



<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/r.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/yaml.min.js"></script>
<script>hljs.configure({languages: []}); hljs.initHighlightingOnLoad();</script>



    
<script src="/js/math-code.js"></script>
<script async src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>


    
  </body>
</html>

